package iswigo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;

public class UtilHttp
{
	public static String PhpSessId = "__utma=196865844.820831841.1306921711.1306921711.1321869095.2; iswigo_sess=6r97idoi31bteklemtabm0pos";

	public static String doPost(String adresse, String data)
	{
		try
		{
			// Send data
			URL url = new URL(adresse);
			URLConnection conn = url.openConnection();
			conn.setDoOutput(true);
			conn.setRequestProperty("Cookie", PhpSessId);
			OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
			wr.write(data);
			wr.flush();
			// Get the response
			BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
			StringBuffer buf = new StringBuffer();
			String line;
			while ((line = rd.readLine()) != null)
			{
				buf.append(line);
			}
			wr.close();
			rd.close();

			return buf.toString();
		}
		catch (Exception e)
		{
		}
		return null;
	}

	public static List<String> getPropertyResponse(String adresse,
			String propertyName)
	{
		URL url = null;
		try
		{
			url = new URL(adresse);
		}
		catch (MalformedURLException e)
		{
			e.printStackTrace();
		}
		HttpURLConnection connection = null;
		try
		{
			connection = (HttpURLConnection) url.openConnection();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		Map<String, List<String>> headerFields = connection.getHeaderFields();

		return headerFields.get(propertyName);
	}

}
